package ui.activity.profit.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import dao.MerchantTradeInput;
import dao.MerchantTradeItem;
import dao.MerchantTradeOutput;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.profit.biz.MerchantTradeBiz;
import ui.activity.profit.contract.MerchantTradeContract;

/**
 * Created by chenjian on 19/2/26
 */

public class MerchantTradePresenter implements MerchantTradeContract.Presenter {

    MerchantTradeContract.View view;
    MerchantTradeBiz merchantTradeBiz;
    List<MerchantTradeItem> list = new ArrayList<>();

    @Inject
    public MerchantTradePresenter(MerchantTradeContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        merchantTradeBiz = (MerchantTradeBiz) biz;
    }


    @Override
    public void getInfo(final boolean isRefresh, String userId,String month) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        MerchantTradeInput input = new MerchantTradeInput();
        input.month = month;
        input.merchantNo = userId;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(input));
        merchantTradeBiz.getTradeListInfo(requestBody, new BaseBiz.Callback<MerchantTradeOutput>() {
            @Override
            public void onSuccess(MerchantTradeOutput info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(MerchantTradeOutput info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void getDayInfo(boolean isRefresh, String userId, String month) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        MerchantTradeInput input = new MerchantTradeInput();
        input.month = month;
        input.merchantNo = userId;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(input));
        merchantTradeBiz.getTradeDayInfo(requestBody, new BaseBiz.Callback<MerchantTradeOutput>() {
            @Override
            public void onSuccess(MerchantTradeOutput info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(MerchantTradeOutput info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

}
